<?php
declare (strict_types=1);

namespace app\api\controller;

use addons\V2\admin\model\OrderTemporary;
use app\common\tool\Result;
use think\facade\View;
use think\Response;

class Order extends Base
{
    /**
     * POST: 支付
     * path: submit
     * method: submit
     * param: address_id - {string} 地址id
     * param: pay_way - {string} 支付方式
     * param: pay_password - {string} 密码
     * param: remark - {string} 备注
     */
    public function submit(): Response
    {
        try {
            $post = $this->request->post();
            $result = OrderTemporary::submit($post, $this->user);
            if ($result['code'] != 1) {
                return Result::fail($result['msg']);
            }

            OrderTemporary::getPayment($result['data']);
            $post['id'] = $result['data'];
            return Result::fastReturn(OrderTemporary::payment($post, $this->user));

        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }


    /**
     * get: 订单列表
     * path: getList
     * method: getList
     * param: page {string} 页数
     * param: type {string} 全部，1待付款，2待发货,3待收货,4已完成
     */
    public function getList(): Response
    {
        try {
            $param = $this->request->get();
            $map = [['uid', '=', $this->user->id]];
            if (isset($param['type']) && $param['type']) {
                $map[] = ['status', '=', $param['type']];
            }

            $list = OrderTemporary::getPageList($map, 'id,sn,total_amount,real_amount,create_time,status,integral,cash,order_type,pay_way', $param['page'],
                $this->getLimit(), false, ['productsField']);

            foreach ($list['data'] as &$v) {
                $v->statusCode = $v->getData('status');
                $v->pay_ways = $v->getData('pay_way');
            }
            return Result::fastReturn($list);
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    /**
     * get: 订单详情
     * path: my_order_details
     * method: my_order_details
     * param: id {string} 订单id
     */
    public function my_order_details()
    {
        $info = OrderTemporary::where(['uid' => $this->user->id, 'id' => $this->request->get('id')])
            ->withoutField('update_time,is_del')
            ->with(['productsField', 'p', 'c', 'd', 'express'])
            ->find();
        if (empty($info)) return redirect('/');
        $info->statusCode = $info->getData('status');
        $info->pay_ways = $info->getData('pay_way');
        $info->friendlyText = getProductOrderFriendly($info->statusCode);
        return Result::success('获取成功', $info);
    }


    /**
     * @param $id
     * 原点 2023年1月5日08:58:04 获取待支付的订单
     */
    public function payment()
    {
        $id = $this->request->param('id');
        $info = OrderTemporary::getPayment($id);
        if (empty($info['data'])) return redirect(url('user/index')->build());
        return Result::success('获取成功', $info['data']['order']);
    }

    /**
     * POST: 支付
     * path: confirmPayment
     * method: confirmPayment
     * param: id - {string} 订单ID
     * param: pay_way - {string} 支付方式
     * param: pay_password - {string} 密码
     */
    public function confirmPayment(): Response
    {
        try {
            $post = $this->request->post();
            $order = \addons\V2\admin\model\Order::find($post['id']);
            $post['pay_way'] = $order->getData('pay_way');
            return Result::fastReturn(OrderTemporary::payment($post, $this->user));
        } catch (\Exception $e) {
            return Result::fail($e->getMessage());
        }
    }

    public function my_order()
    {
        return View::fetch();
    }



    /**
     * POST: 修改状态
     * path: edit
     * method: edit
     * param: id - {string} 订单ID
     * param: type - {string} clear 取消 receipt确认收货
     */
    public function edit(): Response
    {
        return Result::fastReturn(OrderTemporary::edit($this->request->post(), $this->user));
    }


    /**
     * get: 退款原因
     * path: reason
     * method: reason
     */
    public function reason(): Response
    {
        $data = [
            '1'=>"描述和商品不符",
            '2'=>"质量问题",
            '3'=>"卖家发错货",
            '4'=>"未发货",
            '5'=>"拍错了",
        ];
        return Result::success('获取成功',$data);
    }


    /**
     * POST: 退款
     * path: refund
     * method: refund
     * param: id - {string} 订单ID
     * param: reason - {string} 退款原因
     * param: illustrate - {string} 退款说明
     * param: refund_img - {string} 退款说明图片
     */
    public function refund(): Response
    {
        $post = $this->request->post();
        $post['type'] = 'refund';
        return Result::fastReturn(OrderTemporary::edit($post, $this->user));
    }

}


